// by Xeno
//#define __DEBUG__
#define THIS_FILE "x_revive\xr_init.sqf"
#include "xr_macros.sqf"

if (!isNil QGVARXR(INIT)) exitWith {};
GVARXR(INIT) = false;

#define __unc #UNCONSCIOUS

#define __waitpl 0 spawn {waitUntil {!isNull player};GVARXR(INIT) = true}
if (!isDedicated) then {
	if (isNull player) then {
		__waitpl;
	} else {
		GVARXR(INIT) = true;
	};
};

if (GVARXR(with_marker)) then {
	//check markers
	if (isServer) then {
		0 spawn {
			scriptName "spawn_xrrevive_markerserver";
			private ["_mname", "_unit"];
			waitUntil {!isNil QGVARXR(uncon_m_units)};
			waitUntil {count GVARXR(uncon_m_units) > 0};
			while {true} do {
				{
					_mname = _x + __xrdead;
					_unit = __getMNsVar2(_x);
					if (isNull _unit) then {
						[QGVARXR(delml), _mname] call d_fnc_NetCallEvent;
						GVARXR(uncon_m_units) set [_forEachIndex, -1];
					} else {
						if !__GV(_unit,GVARXR(pluncon)) then {
							GVARXR(uncon_m_units) set [_forEachIndex, -1];
						};
					};
					sleep 0.05;
				} forEach GVARXR(uncon_m_units);
				GVARXR(uncon_m_units) = GVARXR(uncon_m_units) - [-1];
				sleep 0.542;
			};
		};
	};
};

if (isDedicated) exitWith {};

waitUntil {GVARXR(INIT)};
waitUntil {player == player};

__pSetVar [QGVARXR(lives), GVARXR(max_lives)];
__pSetVar [QGVARXR(num_death), 0];
__pSetVar [QGVARXR(is_dragging), false];
__pSetVar [QGVARXR(presptime), -1];
__pSetVar [QGVARXR(busyt), -1, true];
__pSetVar [QGVARXR(last_gear_save), -1];
__pSetVar [QGVARXR(pluncon), false, true];
__pSetVar [QGVARXR(pisinaction), false];
__pSetVar [QGVARXR(death_pos), []];
__pSetVar [QGVARXR(dragged), false, true];
__pSetVar [QGVARXR(isdead), false];

/*if (GVARXR(respawn_delay) <= 3) then {
	hint "Attention! Respawn delay in description.ext is too low or missing !!!!!!!!!!!!!!!";
};*/

GVARXR(slopeObj) = "Logic" createVehicleLocal [0,0,0];

GVARXR(pl_group) = group player;
GVARXR(side_pl) = if (!isNull GVARXR(pl_group)) then {side GVARXR(pl_group)} else {playerSide};

GVARXR(strpl) = str player;
GVARXR(strpldead) = GVARXR(strpl) + "_xr_dead";

player addEventHandler ["killed", {_this call FUNCXR(killedEH)}];

player addEventHandler ["respawn", {_this call FUNCXR(respawneh)}];

GVARXR(name_player) = name player;

player addEventHandler [QUOTE(handleDamage), {_this call FUNCXR(ClientHD)}];
/*0 spawn {
	_timeend = time + 10;
	waitUntil {!isNil "BIS_blendColorAlpha" || {time > _timeend}};
	sleep 0.01;
	__TRACE("adding ClientHD EH");
	player removeAllEventHandlers QUOTE(handleDamage);
	player addEventHandler [QUOTE(handleDamage), {_this call FUNCXR(ClientHD)}];
};*/

if (count GVARXR(can_revive) == 0) then {
	GVARXR(pl_can_revive) = true;
} else {
	{
		GVARXR(can_revive) set [_forEachIndex, toUpper _x];
	} forEach GVARXR(can_revive);
	GVARXR(pl_can_revive) = (toUpper(str player) in GVARXR(can_revive));
};

if (GVARXR(with_marker)) then {
	{
		_unit = __getMNsVar2(_x);
		if (!isNil "_unit" && {alive _unit} && {__GV(_unit,GVARXR(pluncon))} && {_unit distance (markerPos QGVARXR(playerparkmarker))> 100} && {side (group _unit) == GVARXR(side_pl)}) then {
			__TRACE_1("with_marker","_unit");
			[(_x + __xrdead), getPosASL _unit, "ICON", "ColorBlue", [0.4,0.4], format [(localize "STR_DOM_MISSIONSTRING_910"), name _unit],0,"mil_marker"] call FUNCXR(CreateMarkerLocal);
		};
	} forEach GVARXR(player_entities);
};

{
	_unit = __getMNsVar2(_x);
	if (!isNil "_unit") then {
		if (alive _unit && {__GV(_unit,GVARXR(pluncon))}) then {
#ifdef __TT__
			if (side (group _unit) == GVARXR(side_pl)) then {
#endif
				_unit call FUNCXR(addActions);
#ifdef __TT__
			};
#endif
		} else {
			_unit setVariable [QGVARXR(ReviveAction), -9999];
			_unit setVariable [QGVARXR(DragAction), -9999];
		};
	};
} forEach GVARXR(player_entities);

waitUntil {!isNull (findDisplay 46)};
__pSetVar [QGVARXR(display_key_handler), (findDisplay 46) displayAddEventHandler ["KeyDown", {_this call FUNCXR(KeyboardHandlerKeyDown)}]];
